home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CU Amiga Super CD-ROM 18
/
CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso
/
CUCD
/
Magazine
/
C_Tutorial
/
Part-6
/
asl2
/
idcmp.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-10-27
|
5KB
|
200 lines
#include "idcmp.h"
#include "drawwin.h"
#include "gadgets.h"
#include "loadsave.h"
#include "menu.h"
#include "toolwin.h"
#include<string.h>
#include<clib/exec_protos.h>
#include<clib/gadtools_protos.h>
#include<clib/graphics_protos.h>
#include<clib/intuition_protos.h>
static void doGadgetUp(struct Window*, UWORD, struct Gadget*);
static int doMenuPick(struct Window*, UWORD);
/* Our message handling code */
void handleIDCMP()
{
char* text = "Hello World!";
int going = TRUE;
int drawing = FALSE;
struct Window* drawwin = getDrawWin();
ULONG drawsig, toolsig, gotsig;
drawsig = 1 << drawwin->UserPort->mp_SigBit;
resetFgPen(drawwin);
while(going)
{
struct IntuiMessage* intuimsg;
/* Only include tool window signal mask if window is open */
toolsig = getToolSig();
/* Wait for messages to arrive */
gotsig = Wait(drawsig | toolsig);
/* Messages have arrived: loop through all of them */
/* Check messages from the drawing window first */
if(gotsig & drawsig)
{
while(intuimsg = GT_GetIMsg(drawwin->UserPort))
{
/* Copy the important bits of the message */
ULONG class = intuimsg->Class;
UWORD code = intuimsg->Code;
WORD mousex = intuimsg->MouseX;
WORD mousey = intuimsg->MouseY;
/* Reply when finished copying bits from message */
GT_ReplyIMsg(intuimsg);
/* Act on this message... */
switch(class)
{
case IDCMP_MOUSEBUTTONS:
switch(code)
{
case SELECTDOWN:
drawing = TRUE;
break;
case SELECTUP:
drawing = FALSE;
break;
}
/* break; omitted so we draw on click, too */
case IDCMP_MOUSEMOVE:
if(drawing)
{
Move(drawwin->RPort, mousex, mousey);
Text(drawwin->RPort, text, strlen(text));
}
break;
case IDCMP_MENUPICK:
going = doMenuPick(drawwin, code);
break;
}
}
}
/* Now check messages from the tool window */
if(going && (gotsig & toolsig))
{
struct Window* toolwin = getToolWin();
while(toolwin && (intuimsg = GT_GetIMsg(toolwin->UserPort)))
{
/* Copy the important bits of the message */
ULONG class = intuimsg->Class;
UWORD code = intuimsg->Code;
APTR iaddr = intuimsg->IAddress;
/* Reply when finished copying bits from message */
GT_ReplyIMsg(intuimsg);
/* Act on this message... */
switch(class)
{
case IDCMP_CLOSEWINDOW:
closeToolWin();
/* Update our local toolwin, so we stop loop */
toolwin = NULL;
uncheckToolBar(drawwin);
break;
case IDCMP_REFRESHWINDOW:
/* You *MUST* remember to ask for and handle these refresh messages */
GT_BeginRefresh(toolwin);
GT_EndRefresh(toolwin, TRUE);
break;
case IDCMP_GADGETUP:
doGadgetUp(drawwin, code, (struct Gadget*)iaddr);
break;
case IDCMP_MENUPICK:
going = doMenuPick(drawwin, code);
/* Update our local toolwin, so we stop loop */
toolwin = getToolWin();
break;
}
}
}
}
}
/* Process IDCMP_GADGETUP event */
static void doGadgetUp(struct Window* drawwin, UWORD code, struct Gadget* gad)
{
switch(gad->GadgetID)
{
case MYBUT_ID:
/* Our button was clicked! Set foreground to next pen colour */
nextFgPen(drawwin);
break;
case MYPAL_ID:
/* Our palette gadget was clicked! Set foreground to gadget colour */
setFgPen(drawwin, code);
break;
}
}
/* Process IDCMP_MENUPICK event */
static int doMenuPick(struct Window* drawwin, UWORD code)
{
struct Menu* menu = drawwin->MenuStrip;
UWORD menuCode, menuNumber, itemNumber;
/* Loop over all the menu selections in the menu code */
struct MenuItem* item;
for(menuCode = code;
menuCode != MENUNULL;
menuCode = item->NextSelect)
{
item = ItemAddress(menu, menuCode);
/* Extract the menu number and menu item number from the menu code */
menuNumber = MENUNUM(menuCode);
itemNumber = ITEMNUM(menuCode);
/* Now decide what to do based on what menu item was selected */
switch(menuNumber)
{
case 0: /* Project menu */
/* Only one item: Quit */
switch(itemNumber)
{
case 0: /* Load */
load(drawwin);
break;
case 1: /* Save */
save(drawwin);
break;
case 3: /* Quit (item 2 is the bar!) */
return FALSE;
}
break;
case 1: /* Pen menu */
switch(itemNumber)
{
case 0: /* Next */
nextFgPen(drawwin);
break;
case 1: /* Prev */
prevFgPen(drawwin);
break;
case 3: /* Reset (item 2 is the bar!) */
resetFgPen(drawwin);
break;
}
break;
case 2: /* Tools menu */
switch(itemNumber)
{
case 0: /* Screen Bar */
ShowTitle(drawwin->WScreen, item->Flags & CHECKED);
break;
case 1: /* Tool Bar */
/* Do the open or close */
if(item->Flags & CHECKED)
{
/* If the open fails, stop immediately */
if(!openToolWin())
return FALSE;
}
else
closeToolWin();
break;
}
}
}
/* Keep going */
return TRUE;
}